<template>
  <div class="brand-container">
    <div class="card-box">
      <el-button type="primary" @click="handleCreate">新增品牌</el-button>
    </div>
    <el-dialog v-model="dialogVisible" :title="formData.id ? '编辑品牌' : '新增品牌'" width="500px">
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
        <el-form-item label="品牌名称" prop="name" required>
          <el-input v-model="formData.name" placeholder="请输入品牌名称" />
        </el-form-item>
        <el-form-item label="品牌Logo" prop="image_url">
          <ImageUpload v-model="formData.image_url" :limit="1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
    <div class="card-box">
      <el-table v-loading="brandStore.loading" :data="brandStore.brandList">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="品牌名称" />
        <el-table-column prop="logo" label="品牌Logo">
          <template #default="{ row }">
            <el-image :src="row.image_url" style="width: 100px; height: 100px" fit="contain" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from "vue";
import useBrandStoreHook from "@/store/modules/goods/brand";
import { ElMessage, ElMessageBox } from "element-plus";
import { BrandQueryForm } from "@/api/goods/brand";
import ImageUpload from "@/components/Upload/ImageUpload.vue";

const brandStore = useBrandStoreHook();

const params = reactive<BrandQueryForm>({
  page: 1,
  size: 10,
});

onMounted(() => {
  brandStore.fetchBrandList(params);
});

const dialogVisible = ref(false);
const formRef = ref();
const formData = reactive({
  id: undefined,
  name: "",
  image_url: "",
});

const rules = reactive({
  name: [
    { required: true, message: "请输入品牌名称", trigger: "blur" },
    { min: 2, max: 20, message: "长度在2到20个字符", trigger: "blur" },
  ],
  image_url: [
    { required: true, message: "请上传品牌Logo", trigger: "blur" },
    { pattern: /.(jpg|jpeg|png|gif)$/, message: "请上传正确的图片格式", trigger: "blur" },
  ],
});

const handleCreate = () => {
  formData.id = undefined;
  formData.name = "";
  formData.image_url = "";
  dialogVisible.value = true;
};

const handleSubmit = async () => {
  try {
    await formRef.value.validate();
    if (formData.id) {
      await brandStore.updateBrand(formData.id, formData);
      ElMessage.success("更新成功");
    } else {
      await brandStore.createBrand(formData);
      ElMessage.success("新增成功");
    }
    dialogVisible.value = false;
  } catch (error) {
    // 表单验证失败
  }
};

const handleEdit = (row: any) => {
  formData.id = row.id;
  formData.name = row.name;
  formData.image_url = row.image_url;
  dialogVisible.value = true;
};

const handleDelete = async (id: number) => {
  try {
    await ElMessageBox.confirm("确认删除该品牌吗？", "提示", {
      type: "warning",
    });
    await brandStore.deleteBrand(id);
    ElMessage.success("删除成功");
  } catch (error) {
    // 用户取消删除
  }
};
</script>

<style scoped>
.brand-container {
  padding: 20px;
}
.header {
  margin-bottom: 20px;
}
</style>
